package com.faf.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Detail;
import com.faf.bean.Food;
import com.faf.bean.Orders;
import com.faf.dao.DetailDao;
import com.faf.dao.FoodDao;
import com.faf.dao.OrdersDao;
import com.faf.dao.impl.DetailDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.OrdersDaoImpl;
import com.faf.service.DetailService;
import com.faf.service.StoreService;
import com.faf.service.impl.DetailServiceImpl;
import com.faf.service.impl.StoreServiceImpl;

/**
 * Servlet implementation class StoreLogin
 */
@WebServlet("/StoreLogin")
public class StoreLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//设置请求的编码格式
		request.setCharacterEncoding("utf-8");
		//设置响应的类型
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String storeNumber=null;
		String password=null;
		int storeId=0;
		try {
			if(request.getParameter("BusinessCenter")!=null) {
				if(request.getParameter("BusinessCenter").equals("1")) {
					
					if(request.getParameter("storeNumber")!=null&&request.getParameter("password")!=null) {
						
						storeNumber = request.getParameter("storeNumber");
						password = request.getParameter("password");
					}
					
					StoreService storeService = new StoreServiceImpl();
					int result = 0;
					result = storeService.CheckBusinessmanLogin(storeNumber,password);
					if(result == 1) {
						request.getSession().setAttribute("storeNumber",storeNumber);
						request.getSession().setAttribute("password",password);
						OrdersDao ordersDao = new OrdersDaoImpl();
						//获得订单号
						storeId = ordersDao.getstoreIdBystoreNumber(storeNumber);
						request.getSession().setAttribute("storeId",storeId);
						
						response.getWriter().print("1");
					}else if(result == -1){
						response.getWriter().print("-1");
					}else {
						response.getWriter().print("0");
					}
					
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void forward(HttpServletRequest request, HttpServletResponse response, String url, Object... objs) throws ServletException, IOException {
		for (int i = 0; i < objs.length; i+=2)
		{
			request.setAttribute(objs[i]+"", objs[i+1]);
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
